const app = getApp()
var flag = false;
Page({
  data: {
    addressId: "",
    addressUserName: "",
    addressBuyerPhone: "",
    addressArea: "",
    addressDetail: "",
    recommendedPrice: "",
    category2Name: "",
    orderRemark: "",

    //预约时间
    serviceTime: ''

  },
  onLoad: function (options) {
    var that = this
    //页面初始化，option为页面跳转所带来的参数
    var openid = wx.getStorageSync("openid");
    var orderId = options.orderId
    console.log("openid", openid)
    console.log("orderId", orderId)
    wx.setStorageSync("orderId0", orderId)
    wx.request({
      url: app.globalData.baseUrl +'/order/findorderuserinfo',
      method: 'GET',
      data: { "buyerOpenid": openid, "orderId": orderId },//传入的参数
      success: function (res) {
        var list = res.data.orderList[0];
        var category2Name = res.data.orderList[0].orders[0].orderDetails[0].category2.category2Name;
        var statusNumber = res.data.orderList[0].orders[0].orderStatus
        
        console.log("list", list)
        console.log("category2Name", category2Name)
        if (list == null) {
          var toastText = '获取数据失败' + res.data.errMsg;
          wx.showToast({
            title: toastText,
            icon: '',
            duration: 2000
          });
        } else {
          that.setData({
            //orderlist: list,
            
            addressUserName: list.addressUserName,
            addressBuyerPhone: list.addressBuyerPhone,
            addressArea: list.addressArea,
            addressDetail: list.addressDetail,
            category2Name: list.orders[0].orderDetails[0].category2.category2Name,
            serviceTime: list.orders[0].serviceTime,
            recommendedPrice: list.orders[0].orderAmount,
            orderRemark: list.orders[0].orderRemark,
            myOpenId: wx.getStorageSync("openid"),
            buyerOpenid: list.buyerOpenid

          });
          ///////////////////图片
          if (list.orders[0].picture != '' && list.orders[0].picture != 'null' && list.orders[0].picture != null) {
            console.log("picture:", list.orders[0].picture)
            var imagesList = list.orders[0].picture.split(",");
            console.log("imagesList:", imagesList)
            for (var i = 0; i < imagesList.length; i++) {
              imagesList[i] = app.globalData.baseUrl + imagesList[i];
            }
            that.setData({

              imagesList: imagesList,

            });

          }
          /////////////
        }
      }
    })

  },

  /**
   * 预览图片方法
   */
  listenerButtonPreviewImage: function (e) {
    let that = this;
    let imagesList = that.data.imagesList;
    let url = e.target.dataset.url;
    console.log(url);

    wx.previewImage({
      current: url,
      urls: imagesList,

      //这根本就不走
      success: function (res) {
        //console.log(res);
      },
      //也根本不走
      fail: function () {
        //console.log('fail')
      }
    })
  },

  onShow: function (options) {


  },
  //点击接单
  
  formSubmit: function (e) {
    var warn = "";//弹框时提示的内容
    var flag = true;//判断信息输入是否完整
    //判断的顺序依次是：姓名-手机号
    /*if (e.detail.value.workerName == "") {
      warn = "请输入您的称呼";
    } else if (!(/^1(3|4|5|7|8)\d{9}$/.test(e.detail.value.workerPhone))) {
      warn = "请输入正确电话";
    } else {*/

      //flag = false;//若必要信息都填写，则不用弹框，且页面可以进行跳转
      var that = this;
      ///////////////////操作数据库，插入订单
      wx.request({
        url: app.globalData.baseUrl +'/order/jiedan',
        data: {//获取数据传到后台
          orderId: wx.getStorageSync("orderId0"),
          
          workerOpenid: wx.getStorageSync("openid")
          //workerName: e.detail.value.workerName,
         // workerPhone: e.detail.value.workerPhone
        },
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          var result = res.data.success
          console.log("result:", result);
          var flag1=false;
          warn=""
          //var toastText = "操作成功！";
          if (result == false) {
            //toastText = "你还不是师傅，请去个人中心设置吧" ;
            flag1 = true
            warn = "您还不是师傅，请去“我的”-->“我是师傅”注册吧";
          }
          if (result == "null"){
            //toastText = "您的评分低于4分，不能接单！"
            flag1 = true
            warn = "您的评分低于2.5分，不能接单！"
          }
          if (result == "fail") {
            //toastText = "您的评分低于4分，不能接单！"
            flag1 = true
            warn = "由于被投诉存在不当行为，您的账号被封禁了，如有疑问请联系客服处理：888888888"
          }
          if (flag1 == true) {
            wx.showModal({
              title: '提示',
              content: warn
            })
          }
          /*wx.showToast({
            title: toastText,
            icon: '',
            duration: 2000
          });*/
          if (result == true) {
            var toastText = "操作成功！";
            wx.showToast({
              title: toastText,
              icon: '',
              duration: 2000
            });
            
            wx.redirectTo({
              url: '../map/map'

            })


          }


        }
      })

      ////////////////////////////////////////////////////


   // }

    //如果信息填写不完整，弹出输入框
    /*if (flag == true) {
      wx.showModal({
        title: '提示',
        content: warn
      })
    }*/


  }
})